﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace DTO.Models
{
    public class SurveyProvider
    {
        CITDBDataContext db = new CITDBDataContext();

        public void save()
        {
            if (db != null)
                db.SubmitChanges();
        }

        public void Update(SURVEY obj)
        {
            SURVEY source = db.SURVEYs.SingleOrDefault(c => c.ID == obj.ID);
            if (obj != null)
            {
                source.Question = obj.Question;
                source.Answer1 = obj.Answer1 == null ? "" : obj.Answer1;
                source.Answer2 = obj.Answer2 == null ? "" : obj.Answer2;
                source.Answer3 = obj.Answer3 == null ? "" : obj.Answer3;
                source.Answer4 = obj.Answer4 == null ? "" : obj.Answer4;
                source.Answer5 = obj.Answer5 == null ? "" : obj.Answer5;
                source.Answer6 = obj.Answer6 == null ? "" : obj.Answer6;
                source.IsTop = obj.IsTop;
                if (source.IsTop == true)
                    Active(obj.ID.ToString());

                save();
            }
        }

        public void UpdateSurvey(SURVEY obj)
        {
            SURVEY source = db.SURVEYs.SingleOrDefault(c => c.ID == obj.ID);
            if (obj != null)
            {
                source.Total1 = obj.Total1;
                source.Total2 = obj.Total2;
                source.Total3 = obj.Total3;
                source.Total4 = obj.Total4;
                source.Total5 = obj.Total5;
                source.Total6 = obj.Total6;
                save();
            }
        }

        public void Insert(SURVEY obj)
        {
            obj.SurveyID = obj.SurveyID == null ? "" : obj.SurveyID;

            obj.Answer3 = obj.Answer3 == null ? "" : obj.Answer3;
            obj.Answer4 = obj.Answer4 == null ? "" : obj.Answer4;
            obj.Answer5 = obj.Answer5 == null ? "" : obj.Answer5;
            obj.Answer6 = obj.Answer6 == null ? "" : obj.Answer6;

            obj.Total1 = obj.Total1 == null ? 0 : obj.Total1;
            obj.Total2 = obj.Total2 == null ? 0 : obj.Total2;
            obj.Total3 = obj.Total3 == null ? 0 : obj.Total3;
            obj.Total4 = obj.Total4 == null ? 0 : obj.Total4;
            obj.Total5 = obj.Total5 == null ? 0 : obj.Total5;
            obj.Total6 = obj.Total6 == null ? 0 : obj.Total6;

            obj.IsTop = obj.IsTop == null ? false : obj.IsTop;
            obj.IsDelete = obj.IsDelete == null ? false : obj.IsDelete;
            obj.DateCreate = obj.DateCreate == null ? DateTime.Now : obj.DateCreate;

            db.SURVEYs.InsertOnSubmit(obj);

            if (obj.IsTop == true)
                Active(obj.ID.ToString());

            save();
        }

        public IQueryable<SURVEY> getSurveyAll()
        {
            return db.SURVEYs.AsQueryable();    
        }

        public SURVEY getSurveyActive()
        {
            return db.SURVEYs.SingleOrDefault(c => c.IsTop == true);
        }

        public SURVEY getSurveyById(int id)
        {
            return db.SURVEYs.SingleOrDefault(c => c.ID == id);
        }

        public IQueryable<SURVEY> getSurvey(DateTime dateTo, DateTime dateForm, bool isTop)
        {
            return db.SURVEYs.Where(c => c.IsDelete == false && c.DateCreate >= dateTo && c.DateCreate <= dateForm.AddDays(1) && c.IsTop == isTop).OrderByDescending(c => c.DateCreate);
        }

        public IQueryable<SURVEY> getSurvey(DateTime dateTo, DateTime dateForm)
        {
            return db.SURVEYs.Where(c => c.IsDelete == false && c.DateCreate >= dateTo && c.DateCreate <= dateForm.AddDays(1)).OrderByDescending(c => c.DateCreate);
        }

        public void DeleteMark(string listid)
        {
            SURVEY obj = null;
            string[] arr = listid.Split('-');

            DataRepository dr = new DataRepository();
            for (int i = 0; i < arr.Length - 1; i++)
            {
                obj = db.SURVEYs.SingleOrDefault(c => c.ID == Convert.ToInt32(arr[i]) && c.IsDelete == false);
                if (obj != null)
                    obj.IsDelete = true;
            }

            save();
        }

        public void Active(string id)
        {
            SURVEY obj = null;
            SURVEY objTop = null;

            DataRepository dr = new DataRepository();

            obj = db.SURVEYs.SingleOrDefault(c => c.ID == Convert.ToInt32(id) && c.IsDelete == false);
            objTop = db.SURVEYs.SingleOrDefault(c => c.IsTop == true && c.IsDelete == false);

            if (objTop != null)
                objTop.IsTop = false;
            
            if (obj != null)
                obj.IsTop = true;

            save();
        }

        #region "web ngoai"
        public SURVEY getSurveyOut()
        {
            try
            {
                return db.SURVEYs.SingleOrDefault(c => c.IsDelete == false && c.IsTop == true);
            }
            catch
            {
                return null;
            }
        }
        #endregion
    }
}